/*  建造者模式的学习    */
#include <iostream>
#include <string>
#include <memory>

using std::cout;
using std::endl;
using std::string;

class Computer
{
public:
    Computer()
    {}

    void setBoard(const string& board)
    {
        _board = board;
    }

    void setDisplat(const string& display)
    {
        _display = display;
    }

    virtual void setOs() = 0;  // 纯虚函数, 让建造者自己来构建

    void showParamaters()
    {
        string param = "computer paramaters: \n";
        param += "\tboard: " + _board + "\n";
        param += "\tdisplay: " + _display + "\n";
        param += "\tOS: " + _os + "\n";
        cout << param << endl;
    }
protected:
    string _board;
    string _display;
    string _os;
};

class MacBock : public Computer
{
public:
    void setOs() override
    {
        _os = "MAC os x12";
    }
};

class Builder
{
public:
    virtual void buildBoard(const string& board) = 0;
    virtual void buildDisplay(const string& display) = 0;
    virtual void buildOs() = 0;
    virtual std::shared_ptr<Computer> build() = 0;
};

class MacBuilder : public Builder
{
public:
    MacBuilder()
        :_computer(new MacBock())
    {}

    void buildBoard(const string& board) override
    {
        _computer->setBoard(board);
    }

    void buildDisplay(const string& display) override
    {
        _computer->setDisplat(display);
    }

    void buildOs() override
    {
        _computer->setOs();
    }

    std::shared_ptr<Computer> build()
    {
        return _computer;
    }
private:
    std::shared_ptr<Computer> _computer;
};

class Director
{
public:
    Director(Builder* builder)
        :_builder(builder)
    {}

    void construct(const string& board, const string& display)
    {
        _builder->buildBoard(board);
        _builder->buildDisplay(display);
        _builder->buildOs();
    }
private:
    std::shared_ptr<Builder> _builder;
};

int main()
{
    Builder* builder = new MacBuilder();
    std::unique_ptr<Director> director(new Director(builder));  // 指挥者来建造对象
    director->construct("华硕主板", "三星显示器");
    std::shared_ptr<Computer> computer = builder->build(); // 建造者建造好对象后要把对象返还回来
    computer->showParamaters();

    return 0;
}
